﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public partial class EngineerFSM : FriendFSM
{
    public float m_fBuildTime = 0.0f;
    bool m_bStuckMove;

    void OnEnable()
    {
        gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.IDLE;

        StartCoroutine(InitState());
    }

    IEnumerator InitState()
    {
        gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.IDLE;
        gameObject.transform.GetChild(0).GetChild(0).GetComponent<UnitHeadState>().SetHeadAnim();

        yield return null;

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);
    }


    void SetState(BASEUNIT_STATE iStateKey)
    {

        switch (iStateKey)
        {
            case BASEUNIT_STATE.IDLE:
                StartCoroutine(IdleState());
                break;
            case BASEUNIT_STATE.MOVE:
                StartCoroutine(MoveState());
                break;
            case BASEUNIT_STATE.COVER:
                StartCoroutine(CoverState());
                break;
            case BASEUNIT_STATE.CLOSEATTACK:
                StartCoroutine(CloseAtkState());
                break;
            case BASEUNIT_STATE.AIM:
                StartCoroutine(AImState());
                break;
            case BASEUNIT_STATE.RELOAD:
                StartCoroutine(ReloadState());
                break;
            case BASEUNIT_STATE.DAMAGE:
                StartCoroutine(DamageState());
                break;
            case BASEUNIT_STATE.DEAD:
                StartCoroutine(DeadState());
                break;
            case BASEUNIT_STATE.BUILD:
                StartCoroutine(BuildState());
                break;
            case BASEUNIT_STATE.STUCKMOVE:
                StartCoroutine(StuckMoveState());
                break;
            case BASEUNIT_STATE.SUPPLY:
                StartCoroutine(SupplyState());
                break;

            default:
                Debug.Log("StateKey Error!");
                StartCoroutine(IdleState());
                break;
        }

    }

    public void StuckCheck()
    {
        List<int> tmpIdxList = Calculate.GetAllSurroundingTile(Calculate.GetMyTileIdx(transform.position), 1);

        for (int i = 0; i < tmpIdxList.Count; ++i)
        {
            if (GameObject.Find("DebugTile").GetComponent<Background>().m_ListTile[tmpIdxList[i]].GetComponent<Tile>().m_iOption == (int)TILE_OPTION.NORMAL)
                return;
        }

        GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.STUCKMOVE;
    }
}
